import QtQuick
import Qt5Compat.GraphicalEffects

import Fk
import Fk.Widgets as W
import Fk.Components.Common

Item {
  id: cardItem
  width : 80
  height : 40
  clip : true

  property bool hovered: false
  property var itemData
  property var card_names
  signal clicked
  signal rightClicked

  property bool enabled: false

  Image {
    anchors.fill: parent
    source: Cpp.path + "/packages/lunarltk-qsgs-ui/image/selfArea/choiceBox/cardname-button.png"
  }

  layer.effect: DropShadow {
    color: "#845422"
    radius: 5
    samples: 25
    spread: 0.7
  }

  Rectangle {
    id : cardImageArea
    anchors.centerIn: parent
    width : parent.width - 8
    height : parent.height - 8
    color: "transparent"
    clip : true
    Image {
      id: cardImage
      sourceSize.width: 93
      sourceSize.height: 130
      // anchors.fill: parent
      anchors.centerIn: parent
      // anchors.topMargin: -20
      source: SkinBank.getCardPicture(cardItem.itemData)
      // fillMode: Image.PreserveAspectCrop
      sourceClipRect: Qt.rect(10, 53, parent.width, parent.height)
      scale : 1.05
    }
  }

  GlowText {
    id : cardName
    text: Lua.tr(itemData)
    visible : true
    font.family: Config.li2Name
    font.pixelSize: 15
    font.bold: true
    color : "#111111"
    glow.color: "#EEEEEE"
    glow.spread: 0.6
    anchors.bottom: parent.bottom
    anchors.right: parent.right
    anchors.rightMargin: 1
  }

  BrightnessContrast {
    anchors.fill: parent
    source: parent
    brightness: parent.enabled ? (parent.hovered ? 0.3 : 0) : -0.5
    visible: !parent.enabled || (parent.enabled && parent.hovered)
  }

  HoverHandler {
    id: hover
    cursorShape: cardItem.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
    onHoveredChanged: {
      if (hovered) {
        cardItem.hovered = true;
      } else {
        cardItem.hovered = false;
      }
    }
  }

  W.TapHandler {
    id: mouse
    acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.NoButton
    gesturePolicy: TapHandler.WithinBounds

    onTapped: (p, btn) => {
      if (cardItem.enabled) {
        if (btn === Qt.LeftButton || btn === Qt.NoButton) {
          cardItem.clicked();
        } else if (btn === Qt.RightButton) {
          cardItem.rightClicked();
        }
      }
    }

    onLongPressed: {
      cardItem.rightClicked();
    }
  }

}